package nl.boelen.game.client.connection;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import nl.boelen.game.client.Client;
import nl.boelen.game.net.packet.Packet;
import nl.boelen.game.utils.Utils;

public class ClientTCPSender {

    private int port;
    private String ipAddress;
    private Client client;

    public ClientTCPSender(Client client, String ipAddress, int port) {
        this.port = port;
        this.ipAddress = ipAddress;
        this.client = client;
    }

    public void sendPacket(final Packet packet) {
        if (client.serverOnline) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    byte[] data = packet.getData();
                    Socket socket = new Socket();
                    try {
                        socket.connect(new InetSocketAddress(ipAddress, port), 5000);
                        try (DataOutputStream out = new DataOutputStream(socket.getOutputStream())) {
                            try (DataInputStream in = new DataInputStream(socket.getInputStream())) {
                                // Send
                                out.writeInt(data.length);
                                out.write(data);
                                // Receive
                                int size = in.readInt();
                                byte[] response = new byte[size];
                                if (size > 0) {
                                    in.readFully(response);

                                    String serverResponse = new String(response).trim();
                                    if (!serverResponse.equalsIgnoreCase("ok")) {
                                        Utils.LOG("Something went wrong in the ClientTCPSendConnection, Received: " + serverResponse);
                                    }
                                }
                            }
                        }
                    } catch (ConnectException | SocketTimeoutException e) {
                        client.serverOnline = false;
                        if (e instanceof ConnectException) {
                            Utils.LOG("Connection to " + ipAddress + " refused, Server Offline");
                        } else {
                            Utils.LOG("Server " + ipAddress + " unreachable");
                        }
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            thread.start();
        }
    }
}
